﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.Drawing;

namespace EXAM
{
    public partial class TestHistory : System.Web.UI.Page
    {
        string sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //.Attributes.Add("onclick", "javascript:history.go(-1);return false");
                //if (Request["T0"] != null)
                //    dgvList.Columns.FindByDataField("ANAL_T0").Visible = false;
                //else dgvList.Columns.FindByDataField("ANAL_T0").Visible = true;
            }
        }
        public string SUNDateString(String val)
        {
            if (val == null || val == "")

                return "";
            int idate = Convert.ToInt32(val);
            DateTime? dDate = BUS.CommonControl.ConvertIntToDate(idate);
            if (dDate == null)
                return "";
            return dDate.Value.ToString("MMM dd,yyyy");
        }
        protected void dgvList_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {

            string t0 = "";
            if (Request["T0"] != null)
                t0 = Request["T0"].ToString();
            BUS.TestControl testCtr = new BUS.TestControl();
            DataTable dt = new DataTable();
            if (Session["UserGuest"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
                //if (t0 != "") dt = testCtr.GetAllByT0(t0, ref sErr);
                //else dt = testCtr.GetAll(ref sErr);
            }
            else
            {
                if (Request["TestID"] != null)
                {
                    DTO.UserInfo urs = Session["UserGuest"] as DTO.UserInfo;
                    BUS.Test_LogControl logCtr = new BUS.Test_LogControl();
                    dt = logCtr.GetAll(urs.ID, Convert.ToInt64(Request["TestID"]), ref sErr);
                    //else dt = testCtr.GetAll(urs.ID, "TST", ref sErr);
                }
                else
                {
                    Response.Redirect("Login.aspx");
                    return;
                }

            }
            dgvList.DataSource = dt;
        }

        protected void dgvList_GroupsChanging(object sender, Telerik.Web.UI.GridGroupsChangingEventArgs e)
        {
            if (e.Action == Telerik.Web.UI.GridGroupsChangingAction.Ungroup)
            {
                foreach (GridGroupByField group in e.Expression.SelectFields)
                {
                    GridColumn col = dgvList.Columns.FindByDataField(group.FieldName);
                    if (col.Visible == false)
                    {
                        col.Visible = true;
                    }
                }
            }
            else if (e.Action == Telerik.Web.UI.GridGroupsChangingAction.Group)
            {
                foreach (GridGroupByField group in e.Expression.SelectFields)
                {
                    GridColumn col = dgvList.Columns.FindByDataField(group.FieldName);
                    if (col.Visible == true)
                    {
                        col.Visible = false;
                    }
                }
            }
        }

        protected void dgvList_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item != null)
            {
                if (e.Item is GridDataItem)
                {
                    if (((DataRowView)((GridDataItem)e.Item).DataItem)["Score"] != null && ((DataRowView)((GridDataItem)e.Item).DataItem)["Score"] != DBNull.Value && Convert.ToDouble(((DataRowView)((GridDataItem)e.Item).DataItem)["Score"].ToString()) >= Convert.ToDouble(((DataRowView)((GridDataItem)e.Item).DataItem)["Pass_Score"].ToString()))
                    {
                        e.Item.ForeColor = Color.Green;
                        ((System.Web.UI.WebControls.ImageButton)e.Item.FindControl("imgResult")).ImageUrl = "images/correct.png";
                        //if (Convert.ToDouble(((DataRowView)((GridDataItem)e.Item).DataItem)["Score"].ToString()) == 100)
                        //    ((System.Web.UI.WebControls.ImageButton)e.Item.FindControl("imgResult")).Enabled = false;
                    }
                    else
                    {
                        //if (((DataRowView)((GridDataItem)e.Item).DataItem)["Score"] == null || ((DataRowView)((GridDataItem)e.Item).DataItem)["Score"] == DBNull.Value)
                        //    ((System.Web.UI.WebControls.ImageButton)e.Item.FindControl("imgResult")).Enabled = false;
                        e.Item.ForeColor = Color.Red; ((System.Web.UI.WebControls.ImageButton)e.Item.FindControl("imgResult")).ImageUrl = "images/wrong.png";
                    }
                }
            }
        }

        

        protected void dgvList_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "HISTORY")
            {
                string id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["No"].ToString();
                Response.Redirect("DetailTest.aspx?TestNo=" + id);
            }
        }
    }
}
